'use strict';
/**
 * 试卷, 知识点, 题型, 同步, 教辅, 专题分别的占比数据
 *
 * created by qiuwei
 * 2016年04月25日
 */

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');

/**
 * @public
 * 创建API请求参数
 * @param  {object} requestData 请求数据
 * @return {object}             请求参数
 */
function request(requestData) {
	// 解析常规筛选条件
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;
	let startDate = requestData.condition.startDate;
	let endDate = requestData.condition.endDate;

	// 题次
	let params = {
		"data": {
			"target": "question_usage-question_num",
			"data_type": "sum",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "question_usage-entry_type",
		"filter": [{
			"item": "question_usage-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "question_usage-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	};

	return [params];
}

/**
 * @public 
 * 响应方法
 * @return {object}              数据对象
 */
function response(data) {

	let rn = {
		series: []
	};

	let map = {
		// 试卷
		103: '试卷',
		// 知识点
		102: '知识点',
		// 题型
		106: '题型',
		// 同步
		104: '同步',
		// 教辅
		101: '教辅',
		// 专题
		105: '专题',
	};

	let counter = Object.keys(map).reduce((counter, key) => {
		counter[key] = 0;
		return counter;
	}, {})

	let result = 0;
	let total = 0;
	data.forEach((item) => {
		if (!isNaN(counter[item.multi_data])) {
			result = parseInt(item.result);

			counter[item.multi_data] += result;
			total += result;
		}
	});

	if (total > 0) {
		let value;
		for (let key in map) {
			value = map[key];

			rn.series.push({
				name: value,
				y: Math.round(counter[key] / total * 10000) / 100
			});
		}
	}

	return rn;
}

/**
 * @private
 * 解析常规筛选条件
 * @param  {object} condition 筛选条件对象
 * @return {array}            条件数组
 */
function filter(condition) {
	let filters = [];

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('teacher-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('teacher-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('teacher-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('teacher-school_id', '=', condition.school));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('teacher-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('teacher-source', 'in', condition.deviceType.join(',')));
	}

	filters.push(builder.append('teacher-student_count', '>=', condition.userFeature));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
}